package com.zzmcc.wechat.service.impl;

import com.zzmcc.wechat.bean.InMessage;
import com.zzmcc.wechat.bean.NewsOutMessage;
import com.zzmcc.wechat.bean.OutMessage;
import com.zzmcc.wechat.bean.TextOutMessage;
import com.zzmcc.wechat.entity.Account;
import com.zzmcc.wechat.entity.Articles;
import com.zzmcc.wechat.entity.MsgTypes;
import com.zzmcc.wechat.oauth.Oauth;
import com.zzmcc.wechat.service.AccountService;
import com.zzmcc.wechat.service.MessageProcessingHandler;
import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.StringUtil;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class DefaultMessageProcessingHandlerImpl implements MessageProcessingHandler {
    @Inject
    private  AccountService accountService;
	private OutMessage outMessage;

    @Override
    public OutMessage getOutMessage(InMessage msg) {
       MsgTypes msgTypes = MsgTypes.getMsgTypes(msg.getMsgType());
       switch (msgTypes){
           case TEXT:
               textTypeMsg(msg);
               break;
           case LOCATION:
               locationTypeMsg(msg);
               break;
           case IMAGE:
               imageTypeMsg(msg);
               break;
           case LINK:
               linkTypeMsg(msg);
               break;
           case VOICE:
               voiceTypeMsg(msg);
               break;
           case EVENT:
               eventTypeMsg(msg);
               break;
           case VIDEO:
               videoTypeMsg(msg);
               break;
           default:
               textTypeMsg(msg);

       }

        return outMessage;
    }
    @Override
    public void setOutMessage(OutMessage outMessage) {
        this.outMessage = outMessage;
    }

    @Override
    public void afterProcess(InMessage inMessage,OutMessage outMessage) {

    }

	@Override
	public void allType(InMessage msg){
        TextOutMessage out = new TextOutMessage();
        out.setContent("您的消息已经收到！");
        setOutMessage(out);
	}

	@Override
	public void textTypeMsg(InMessage msg) {

        TextOutMessage out = new TextOutMessage();
        out.setContent("您的文本消息已经收到！");
        setOutMessage(out);

	}

	@Override
	public void locationTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        out.setContent("您的位置信息已经收到！");
        setOutMessage(out);
	}

	@Override
	public void imageTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        out.setContent("您的图片信息已经收到！");
        setOutMessage(out);
	}

	@Override
	public void videoTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        out.setContent("您的视频已经收到！");
        setOutMessage(out);
	}

	@Override
	public void voiceTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        if(StringUtil.isNotEmpty(msg.getRecognition()))
            out.setContent( msg.getRecognition());
        else
            out.setContent("无法识别你的语音");
        setOutMessage(out);
	}

	@Override
	public void linkTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        out.setContent("您的链接信息已经收到！");
        setOutMessage(out);
	}

	@Override
	public void eventTypeMsg(InMessage msg) {
        TextOutMessage out = new TextOutMessage();
        String event = msg.getEvent().toLowerCase();
        if (event.equals("subscribe"))
            out.setContent("感谢您的关注！");
        else if (event.equals("unsubscribe"))
            out.setContent("欢迎您继续关注！");
        else if(event.equals("location"))
            out.setContent("您的位置信息：");
        else
            out.setContent("您的事件信息已经收到！");
        NewsOutMessage newsOutMessage=new NewsOutMessage();

       // Account account = accountService.findAccount(5);
        String appid="wx13299d5a69b39723";
        String secret="2ac824248c90219af2d4426bcf1f06a6";
        Oauth oauth=new Oauth(appid,secret);
        String code =oauth.getCode();


        Articles articles=new Articles();
        articles.setTitle("网页授权");
        articles.setDescription("网页授权");
        articles.setPicUrl("");
        articles.setUrl(code);

        List<Articles> articlesList =new ArrayList<Articles>();
        articlesList.add(articles);
        newsOutMessage.setArticles(articlesList);




        setOutMessage(newsOutMessage);
    }



}
